package com.ruoyi.web.controller.module.examine;

import java.util.List;
import java.util.Arrays;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.system.domain.module.estate.Estate;
import com.ruoyi.system.domain.module.property.Property;
import com.ruoyi.system.domain.module.examine.ExamineApproveTemplate;
import com.ruoyi.system.domain.module.examine.TemplateTree;
import com.ruoyi.system.service.module.estate.IEstateService;
import com.ruoyi.system.service.module.property.IPropertyService;
import com.ruoyi.system.service.module.examine.IExamineApproveTemplateService;
import com.ruoyi.web.controller.common.BaseController;
import lombok.RequiredArgsConstructor;
import javax.validation.constraints.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 审批流程模板Controller
 *
 * @author ruoyi
 * @date 2022-11-23
 */
@Validated
@Api(value = "审批流程模板控制器", tags = {"审批流程模板管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/module/approve/template")
public class ExamineApproveTemplateController extends BaseController {

    private final IExamineApproveTemplateService examineApproveTemplateService;
    private final IPropertyService propertyService;
    private final IEstateService estateService;

    /**楼宇
     * 查询审批流程模板列表
     */
    @ApiOperation("查询审批流程模板列表")
    @PreAuthorize("@ss.hasPermi('module:template:list')")
    @GetMapping("/list")
    public TableDataInfo<ExamineApproveTemplate> list(@Validated ExamineApproveTemplate examineApproveTemplate) {
		SysUser sysUser = getUser();
        QueryWrapper<ExamineApproveTemplate> qw = new QueryWrapper<>();
        if (examineApproveTemplate.getPropertyId()!=null){
            qw.eq("property_id",examineApproveTemplate.getPropertyId());
        }
		if(sysUser.getUserType() == 2){
			qw.eq("property_id",sysUser.getPropertyId());
			if (sysUser.getEstateId()!=null){
				qw.eq("estate_id",sysUser.getEstateId());
			}
		}else if(sysUser.getUserType() == 3){
			qw.eq("estate_id",sysUser.getEstateId());
		}
        if (examineApproveTemplate.getEstateId()!=null){
            qw.eq("estate_id",examineApproveTemplate.getEstateId());
        }
        TableDataInfo<ExamineApproveTemplate> td = PageUtils.buildDataInfo(examineApproveTemplateService.page(PageUtils.buildPage(),qw));
        List<ExamineApproveTemplate> list = td.getRows();
        return td;
    }

    /**
     * 导出审批流程模板列表
     */
    @ApiOperation("导出审批流程模板列表")
    @PreAuthorize("@ss.hasPermi('module:template:export')")
    @Log(title = "审批流程模板", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<ExamineApproveTemplate> export(@Validated ExamineApproveTemplate examineApproveTemplate) {
        List<ExamineApproveTemplate> list = examineApproveTemplateService.list();
        ExcelUtil<ExamineApproveTemplate> util = new ExcelUtil<ExamineApproveTemplate>(ExamineApproveTemplate.class);
        return util.exportExcel(list, "审批流程模板");
    }



	/**
	 * 查询预案策略下的组织机构分组列表
	 */
	@PreAuthorize("@ss.hasPermi('module:template:list')")
	@GetMapping("/nodeList")
	public AjaxResult nodeList(@Validated ExamineApproveTemplate examineApproveTemplate) {
		SysUser user = getUser();
		TemplateTree tree = examineApproveTemplateService.getTreeByTemplateId(examineApproveTemplate);
		return AjaxResult.success(tree);
	}

    /**
     * 获取审批流程模板详细信息
     */
    @ApiOperation("获取审批流程模板详细信息")
    @PreAuthorize("@ss.hasPermi('module:template:query')")
    @GetMapping("/{id}")
    public AjaxResult<ExamineApproveTemplate> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        ExamineApproveTemplate examineApproveTemplate = examineApproveTemplateService.getById(id);
        return AjaxResult.success(examineApproveTemplate);
    }

    /**
     * 新增审批流程模板
     */
    @ApiOperation("新增审批流程模板")
    @PreAuthorize("@ss.hasPermi('module:template:add')")
    @Log(title = "审批流程模板", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated(AddGroup.class) @RequestBody ExamineApproveTemplate examineApproveTemplate) {
        if(examineApproveTemplate.getPropertyId()!=null){
            Property property = propertyService.findById(examineApproveTemplate.getPropertyId());
            if (property!=null){
                examineApproveTemplate.setPropertyName(property.getPropertyName());
            }
        }
        if (examineApproveTemplate.getEstateId()!=null){
            Estate estate = estateService.findById(examineApproveTemplate.getEstateId());
            if (estate!=null){
                examineApproveTemplate.setEstateName(estate.getEstateName());
            }
        }
        return toAjax(examineApproveTemplateService.save(examineApproveTemplate) ? 1 : 0);
    }

    /**
     * 修改审批流程模板
     */
    @ApiOperation("修改审批流程模板")
    @PreAuthorize("@ss.hasPermi('module:template:edit')")
    @Log(title = "审批流程模板", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated(EditGroup.class) @RequestBody ExamineApproveTemplate examineApproveTemplate) {
        if(examineApproveTemplate.getPropertyId()!=null){
            Property property = propertyService.findById(examineApproveTemplate.getPropertyId());
            if (property!=null){
                examineApproveTemplate.setPropertyName(property.getPropertyName());
            }
        }
        if (examineApproveTemplate.getEstateId()!=null){
            Estate estate = estateService.findById(examineApproveTemplate.getEstateId());
            if (estate!=null){
                examineApproveTemplate.setEstateName(estate.getEstateName());
            }
        }
        return toAjax(examineApproveTemplateService.updateById(examineApproveTemplate) ? 1 : 0);
    }

    /**
     * 删除审批流程模板
     */
    @ApiOperation("删除审批流程模板")
    @PreAuthorize("@ss.hasPermi('module:template:remove')")
    @Log(title = "审批流程模板" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return toAjax(examineApproveTemplateService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
